<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
            v-model="queryParams.title"
            clearable
            placeholder="请输入标题"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="饲养员" prop="feeder">
        <el-input
            v-model="queryParams.feeder"
            clearable
            placeholder="请输入饲养员"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="孵化批次" prop="hatchBatch">
        <el-input
            v-model="queryParams.hatchBatch"
            clearable
            placeholder="请输入孵化批次"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="鸡舍编号" prop="coopNumber">
        <el-input
            v-model="queryParams.coopNumber"
            clearable
            placeholder="请输入鸡舍编号"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['feedschedule:feedschedule:add']"
            icon="Plus"
            plain
            type="primary"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['feedschedule:feedschedule:edit']"
            :disabled="single"
            icon="Edit"
            plain
            type="success"
            @click="handleUpdate"
        >编辑
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['feedschedule:feedschedule:remove']"
            :disabled="multiple"
            icon="Delete"
            plain
            type="danger"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['feedschedule:feedschedule:export']"
            icon="Download"
            plain
            type="warning"
            @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedscheduleList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="35"/>
      <el-table-column align="center" label="序号" type="index" width="50px"></el-table-column>
      <el-table-column align="center" label="标题" prop="title" width="80px"/>
      <el-table-column align="center" label="饲养员" prop="feeder" width="80px"/>
      <el-table-column align="center" label="孵化批次" prop="hatchBatch" width="80px"/>
      <el-table-column align="center" label="当日饲料总量" prop="feedTotal" width="120px"/>
      <el-table-column align="center" label="饲料金额" prop="feedCost" width="80px"/>
      <el-table-column align="center" label="鸡群数量" prop="chickenCount" width="80px"/>
      <el-table-column align="center" label="鸡舍编号" prop="coopNumber" width="80px"/>
      <el-table-column align="center" label="审核状态" prop="reviewStatus">
        <template #default="scope">
          <dict-tag :options="document_status" :value="scope.row.reviewStatus"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="说明备注" prop="remark" width="120px"/>
      <el-table-column align="center" label="审核时间" prop="reviewTime" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.reviewTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="审核备注" prop="reviewRemark" width="80px"/>
      <el-table-column align="center" label="审核人" prop="reviewer" width="80px"/>
      <el-table-column align="center" label="更新者" prop="updatedBy"/>
      <el-table-column align="center" label="更新日期" prop="updateTime" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建者" prop="createdBy" width="100"/>
      <el-table-column align="center" label="创建时间" prop="createTime" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="130px">
        <template #default="scope">
          <el-button v-hasPermi="['feedschedule:feedschedule:edit']" icon="Edit" link type="primary"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-hasPermi="['feedschedule:feedschedule:remove']" icon="Delete" link type="primary"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改鸡场投喂信息对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="700px">
      <el-form ref="feedscheduleRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="饲养员" prop="feeder">
              <el-input v-model="form.feeder" placeholder="请输入饲养员"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="孵化批次" prop="hatchBatch">
              <el-input v-model="form.hatchBatch" placeholder="请输入孵化批次"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="饲料总量" prop="feedTotal">
              <el-input v-model="form.feedTotal" placeholder="请输入当日饲料总量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="饲料金额" prop="feedCost">
              <el-input v-model="form.feedCost" placeholder="请输入饲料金额"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="鸡群数量" prop="chickenCount">
              <el-input v-model="form.chickenCount" placeholder="请输入鸡群数量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="鸡舍编号" prop="coopNumber">
              <el-input v-model="form.coopNumber" placeholder="请输入鸡舍编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="审核状态" prop="reviewStatus">
              <el-select v-model="form.reviewStatus" placeholder="请选择审核状态">
                <el-option
                    v-for="dict in document_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="说明备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入说明备注"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="审核时间" prop="reviewTime">
              <el-date-picker v-model="form.reviewTime"
                              clearable
                              placeholder="请选择审核时间"
                              type="date"
                              value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="审核人" prop="reviewer">
              <el-input v-model="form.reviewer" placeholder="请输入审核人"/>
            </el-form-item>

          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="审核备注" prop="reviewRemark">
              <el-input v-model="form.reviewRemark" placeholder="请输入审核备注"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="创建者" prop="createdBy">
              <el-input v-model="form.createdBy" placeholder="请输入创建者"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Feedschedule" setup>
import {
  addFeedschedule,
  delFeedschedule,
  getFeedschedule,
  listFeedschedule,
  updateFeedschedule
} from "@/api/feedschedule/feedschedule";

const {proxy} = getCurrentInstance();
const {document_status} = proxy.useDict('document_status');

const feedscheduleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    feeder: null,
    hatchBatch: null,
    coopNumber: null,
  },
  rules: {
    title: [
      {required: true, message: "标题不能为空", trigger: "blur"}
    ],
    feeder: [
      {required: true, message: "饲养员不能为空", trigger: "blur"}
    ],
    hatchBatch: [
      {required: true, message: "孵化批次不能为空", trigger: "blur"}
    ],
    feedTotal: [
      {required: true, message: "当日饲料总量不能为空", trigger: "blur"}
    ],
    feedCost: [
      {required: true, message: "饲料金额不能为空", trigger: "blur"}
    ],
    chickenCount: [
      {required: true, message: "鸡群数量不能为空", trigger: "blur"}
    ],
    coopNumber: [
      {required: true, message: "鸡舍编号不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询鸡场投喂信息列表 */
function getList() {
  loading.value = true;
  listFeedschedule(queryParams.value).then(response => {
    feedscheduleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    feeder: null,
    hatchBatch: null,
    feedTotal: null,
    feedCost: null,
    chickenCount: null,
    coopNumber: null,
    reviewStatus: null,
    remark: null,
    reviewTime: null,
    reviewRemark: null,
    reviewer: null,
    updatedBy: null,
    updateTime: null,
    createdBy: null,
    createTime: null
  };
  proxy.resetForm("feedscheduleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加鸡场投喂信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFeedschedule(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改鸡场投喂信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["feedscheduleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFeedschedule(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFeedschedule(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除鸡场投喂信息编号为"' + _ids + '"的数据项？').then(function () {
    return delFeedschedule(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('feedschedule/feedschedule/export', {
    ...queryParams.value
  }, `feedschedule_${new Date().getTime()}.xlsx`)
}

getList();
</script>
